<template>
  <div
    class="contact-item"
    :class="{ 'contact-item--selected': isSelected }"
    role="button"
    tabindex="0"
    :aria-label="`联系人 ${contact.name}, 状态: ${contact.status}`"
    @click="handleSelect"
    @keydown.enter="handleSelect"
    @keydown.space.prevent="handleSelect"
  >
    <div class="contact-avatar">
      <Avatar
        :src="contact.avatar"
        :status="contact.status"
        :username="contact.name"
        size="medium"
      />
    </div>

    <ContactInfo :contact="contact" />

    <ContactActions
      :contact="contact"
      @send-message="handleSendMessage"
      @view-contact="handleViewContact"
      @call="handleCall"
      @video-call="handleVideoCall"
      @more-actions="handleMoreActions"
    />
  </div>
</template>

<script setup lang="ts">
import Avatar from '@/libs/Avatar/Avatar.vue'
import ContactInfo from './ContactInfo.vue'
import type { Contact } from '../types'
import ContactActions from './ContactActions.vue'

interface Props {
  contact: Contact
  isSelected?: boolean
}

interface Emits {
  (e: 'select', contact: Contact): void
  (e: 'send-message', contact: Contact): void
  (e: 'view-contact', contact: Contact): void
  (e: 'call', contact: Contact): void
  (e: 'video-call', contact: Contact): void
  (e: 'more-actions', contact: Contact): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const handleSelect = (): void => {
  emit('select', props.contact)
}

const handleSendMessage = (contact: Contact): void => {
  emit('send-message', contact)
}

const handleViewContact = (contact: Contact): void => {
  emit('view-contact', contact)
}

const handleCall = (contact: Contact): void => {
  emit('call', contact)
}

const handleVideoCall = (contact: Contact): void => {
  emit('video-call', contact)
}

const handleMoreActions = (contact: Contact): void => {
  emit('more-actions', contact)
}
</script>

<style scoped>
.contact-item {
  display: flex;
  align-items: center;
  padding: 16px;
  background-color: var(--background-color-secondary);
  border: 1px solid var(--border-color-light);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.contact-item:hover {
  background-color: var(--background-color-tertiary);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.contact-item--selected {
  background-color: var(--primary-color-light, rgba(74, 144, 226, 0.1));
  border-color: var(--primary-color);
}

.contact-avatar {
  margin-right: 16px;
  flex-shrink: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .contact-item {
    padding: 12px;
  }

  .contact-avatar {
    margin-right: 12px;
  }
}

@media (max-width: 480px) {
  .contact-item {
    padding: 10px;
  }

  .contact-avatar {
    margin-right: 10px;
  }
}
</style>
